package bgu.eliav;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

public class NewUserUI extends Activity {
	static final int DIALOG_EXIT = 0;
	static final int DIALOG_ALERT = 1;
	static final int DATE_DIALOG = 2;
	public int numOfDig = 0;
	private EditText birthDateField;

	private int mDay = 0;
	private int mMonth = 0;
	private int mYear = 0;
	final NewUserUI newUser = this;
    public  ProgressDialog progressDialog;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final Activity newUser = this;
		final Calendar calendar = Calendar.getInstance();
		setContentView(R.layout.user_detail);
		birthDateField = (EditText) findViewById(R.id.EditBirthDate);
		birthDateField.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				mDay = calendar.get(Calendar.DAY_OF_MONTH);
				mMonth = calendar.get(Calendar.MONDAY);
				mYear = calendar.get(Calendar.YEAR);
				showDialog(DATE_DIALOG);

			}
		});

		Button AddUIButton = (Button) this.findViewById(R.id.AddItemUIButton);
		AddUIButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String name = "";
				String id = "";
				String phone = "";
				String address = "";

				EditText editName = (EditText) findViewById(R.id.EditName);
				EditText editID = (EditText) findViewById(R.id.EditID);
				EditText editPhone = (EditText) findViewById(R.id.EditPhone);
				EditText editAddress = (EditText) findViewById(R.id.EditAddress);
				
				name = editName.getText().toString();
				id = editID.getText().toString();
				phone = editPhone.getText().toString();
				address = editAddress.getText().toString();
				String bdate = mDay + "/" + mMonth + "/" + mYear;
				if (id.length() != 9) {
					// show alert about the wrong input
					numOfDig = id.length();
					showDialog(DIALOG_ALERT);
				}

				else {
					User user = new User(name, id, phone, address, bdate);
					DatabaseExample db = DatabaseExample.getInstance(newUser);
					db.newUser(name, id, phone, address, bdate, newUser);
					// DatabaseExample.newUser(name, id, phone, address, bdate,
					// newUser) ;
					//finish();

				}
			}
		});

	}
	
	private void stam(){
		this.progressDialog = ProgressDialog.show(this, "working . . .", "performing HTTP request"); 
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu1, menu);
		return true;

	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.exit:
			showDialog(DIALOG_EXIT);

		}

		return super.onOptionsItemSelected(item);
	}

	// The callback used to indicate the user is done filling in the date.
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		// press set on the dialog
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;

			updateDisplay();

		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case DIALOG_EXIT:
			return creatingExitDialog();

		case DIALOG_ALERT:
			return creatingAlertDialog("Wrong input you enter  " + numOfDig
					+ " instead of 9\n please enter again ");

		case DATE_DIALOG:
			return createDatePickerDialog();

		}

		return dialog;
	}

	private Dialog creatingExitDialog() {
		final Activity localActivity = this;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to exit?");
		builder.setCancelable(false);

		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				SharedPreferences ref = getSharedPreferences("bgu.eliav",
						MODE_PRIVATE);
				SharedPreferences.Editor ed = ref.edit();
				ed.putString("action", "exit");
				ed.commit();
				localActivity.finish();
			}
		});

		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		return alert;

	}

	private Dialog creatingAlertDialog(String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg).setCancelable(true).setTitle("Alert!");
		AlertDialog alert = builder.create();
		return alert;
	}

	private DatePickerDialog createDatePickerDialog() {
		return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
	}

	// update the field
	private void updateDisplay() {
		this.birthDateField.setText(new StringBuilder().append(mDay)
				.append("/").append(mMonth + 1).append("/").append(mYear));

	}

}
